//
//  TableViewController.swift
//  AliBaiChuan
//
//  Created by 郑东喜 on 2017/2/17.
//  Copyright © 2017年 郑东喜. All rights reserved.
//

import UIKit

class TableViewController: UITableViewController {
    
    lazy var shareBtn: UIButton = {
        let btn : UIButton = UIButton.init(frame: CGRect.init(x: SW * 0.25, y: SH * 0.75, width: 50, height: 50))
        btn.backgroundColor = .gray
        btn.layer.cornerRadius = btn.frame.width * 0.5
        return btn
    }()

    lazy var ss: NSMutableArray = {
        let s :NSMutableArray = ["购物车","订单号"]
        return s
    }()
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        print("\((#file as NSString).lastPathComponent):(\(#line))\n",self.navigationController?.childViewControllers.count)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationController?.navigationBar.barTintColor = UIColor.orange
    }
    
    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if ss.count == 0 {
            return 1
        } else {
            return ss.count
        }
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cellID")
        
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "cellID")
        }
        
        cell?.textLabel?.text = "家鼠 - \(indexPath.row)"
        cell?.textLabel?.font = UIFont.init(name: "Sketch 3D", size: 14)
        
        return cell!
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
            //购物车
        case 0:
            self.hidesBottomBarWhenPushed = true
            
            self.openItemDetatilMyPage(itemId: "543256891698")

//            self.alSelfWebView(itemId: "543256891698")
            
            self.hidesBottomBarWhenPushed = false

            break
            
            //订单
        case 1:
            
            
            
            self.hidesBottomBarWhenPushed = true

            self.navigationController?.pushViewController(ShopVC(), animated: true)
            
            self.hidesBottomBarWhenPushed = false
            
            break
        default:
            break
        }
    }
    deinit {
        print("\((#file as NSString).lastPathComponent):(\(#line))")
    }
}

// MARK:- 购物车
extension TableViewController {
    fileprivate func shopCar() -> Void {
        let page = AlibcTradePageFactory.myCartsPage()
        
        let showParams : AlibcTradeShowParams = AlibcTradeShowParams()
        showParams.openType = .H5
        showParams.isNeedPush = true
        showParams.openType = ALiOpenType.H5
        
        let aVC = WebView()
        
        self.navigationController?.pushViewController(aVC, animated: true)
        
        AlibcTradeSDK.sharedInstance().tradeService().show(showParams.isNeedPush ? self.navigationController! : self, webView: aVC.weView, page: page, showParams: showParams, taoKeParams: nil, trackParam: nil, tradeProcessSuccessCallback: { (result) in
            
        }) { (error) in

        }
    }
}

// MARK:- 商品详情页
extension TableViewController {
    //用自己的webview打开商品详情界面
    func openItemDetatilMyPage(itemId:String) {
        var page = AlibcTradePageFactory.myCartsPage()
        
        let showParams : AlibcTradeShowParams = AlibcTradeShowParams()
        showParams.openType = .H5
        showParams.isNeedPush = true
        
        
        page = AlibcTradePageFactory.itemDetailPage(itemId)
        showParams.openType = ALiOpenType.H5
        
        let aVC = BacWebView()

        
        self.navigationController?.pushViewController(aVC, animated: true)
    
        AlibcTradeSDK.sharedInstance().tradeService().show(showParams.isNeedPush ? self.navigationController! : self, webView: aVC.weView, page: page, showParams: showParams, taoKeParams: nil, trackParam: nil, tradeProcessSuccessCallback: { (result) in

        }) { (error) in

        }
    
    }
    
    func alSelfWebView(itemId:String) {
        
        var page = AlibcTradePageFactory.myCartsPage()
        
        let showParams : AlibcTradeShowParams = AlibcTradeShowParams()
        showParams.openType = .H5
        showParams.isNeedPush = true
        
        
        page = AlibcTradePageFactory.itemDetailPage(itemId)
        showParams.openType = ALiOpenType.H5
        
        AlibcTradeSDK.sharedInstance().tradeService().show(showParams.isNeedPush ? self.navigationController! : self, page: page, showParams: showParams, taoKeParams: nil, trackParam: nil, tradeProcessSuccessCallback: { (_) in
            
        }) { (_) in
            
        }
    }
}

